package dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import beans.LogPayPackage;

import utils.HibernateUtil;




public class LogPayPackageDao {
	private SessionFactory sessionFactory;

	public LogPayPackageDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	ExpressDao expressDao = new ExpressDao();
	Transaction tr = null;
	
	/**
	 * 删除扣费日志
	 */
	public void delete(LogPayPackage logPayPackage) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(logPayPackage);
		tr.commit();
		session.close();
	}
	
	/**
	 * 修改扣费日志
	 */
	public void edit(LogPayPackage logPayPackage) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(logPayPackage);
		tr.commit();
		session.close();
	}
	
	/**
	 * 保存新增的扣费日志
	 */
	public void save(LogPayPackage logPayPackage) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(logPayPackage);
		tr.commit();
		session.close();
	}
	
	/**
	 * 返回所有扣费日志
	 */
	public List<LogPayPackage> getAllLogPayPackage(){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(LogPayPackage.class);
		List<LogPayPackage> logPayPackages=c.list();
		tr.commit();
		session.close();
		return logPayPackages;
	}
	
	/**
	 * 根据快递网点ID返回所有扣费日志
	 */
	public List<LogPayPackage> getByExpBranchID(int expBranchID){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(LogPayPackage.class);
		c.add(Restrictions.eq("express", expressDao.getExpressBranch(expBranchID)));
		c.addOrder(Order.desc("payTime"));
		List<LogPayPackage> logPayPackages=c.list();
		tr.commit();
		session.close();
		return logPayPackages;
	}
	
	/**
	 * 通过扣费日志ID返回指定扣费日志
	 * 
	 * @return
	 */
	public LogPayPackage getLogPayPackageById(int logPayPackageID) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		LogPayPackage logPayPackage = (LogPayPackage) session.get(LogPayPackage.class, logPayPackageID);
		tr.commit();
		session.close();
		return logPayPackage;
	}
}
